﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.service.auth;
using System.Collections.Generic;
using com.pro2e.console.service.auth.vo;
using com.pro2e.console.service.msg.vo;
using bgo.web.ui;
using com.pro2e.web.util.page;

public partial class adm_UC14_UC14_4_RoleFuncSet : System.Web.UI.Page
{
    AuthService authService = new AuthService();
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initData();

        }

        //gvAuth.Rows

    }

    private void initData()
    {
        initDDLRole();
        initDDLMenuFunc();
        initGridView();
    }



    private void initDDLMenuFunc()
    {
        IList<MenuFunc> MenuFuncList = authService.GetTopMenuFunc();


        foreach (MenuFunc menu in MenuFuncList)
        {
            ListItem item = new ListItem(menu.MenuFuncName, menu.Id.ToString());
            ddlMenuFunc.Items.Add(item);
        }
        ddlMenuFunc.Items.Add(new ListItem("全部", "0"));
        ddlMenuFunc.DataBind();


    }

    private void initDDLRole()
    {
        IList<LoginRole> roleList = authService.GetAllRoles();

        foreach (LoginRole role in roleList)
        {
            ListItem item = new ListItem(role.RoleName, role.RoleId.ToString());
            ddlRole.Items.Add(item);
        }

        ddlRole.DataBind();
    }

    private void initGridView()
    {
        btnSearch_Click(null, null);
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        string selectedRole = ddlRole.SelectedValue;
        string selectedMenuFunc = ddlMenuFunc.SelectedValue;
        IList<RoleMenuShowVO> showVOList = new List<RoleMenuShowVO>();

        log.Debug("selectedRole=" + selectedRole);
        log.Debug("selectedMenuFunc=" + selectedMenuFunc);

        LoginRole role = authService.GetLoginRole(int.Parse(selectedRole));

        IList<MenuFunc> selectedMenuFuncList;


        if (selectedMenuFunc.Equals("0"))
        {
            selectedMenuFuncList = authService.GetNotTopMenuFunc();
        }
        else
        {
            selectedMenuFuncList = authService.GetMenuFuncByParentId(int.Parse(selectedMenuFunc));
        }

        foreach (MenuFunc menu in selectedMenuFuncList)
        {
            RoleMenuShowVO showVO = new RoleMenuShowVO();

            showVO.Id = menu.Id;
            showVO.Name = menu.MenuFuncName;
            showVO.No = menu.Note;
            if (role.MenuFuncs.Contains(menu))
            {
                showVO.IsAuth = true;
            }
            else
            {
                showVO.IsAuth = false;
            }

            showVOList.Add(showVO);
        }

        gvAuth.DataSource = showVOList;
        gvAuth.DataBind();
    }

    protected void ddlMenuFunc_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSearch_Click(null, null);
    }
    protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSearch_Click(null, null);
    }

    protected void ckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ckAll = (CheckBox)sender;

        if (ckAll.ID.Equals("ckAll"))
        {
            foreach (GridViewRow row in gvAuth.Rows)
            {
                Control ctrl = row;

                UIHelper.SetCheckBoxChecked(ref ctrl, "ckIsAuth", ckAll.Checked);
            }
        }
    }

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        string selectedRole = ddlRole.SelectedValue;
        LoginRole role = authService.GetLoginRole(int.Parse(selectedRole));

        foreach (GridViewRow row in gvAuth.Rows)
        {
            CheckBox ckAuth = (CheckBox)row.Cells[2].FindControl("ckIsAuth");
            HiddenField hdnId = (HiddenField)row.Cells[2].FindControl("hdnId");

            if (ckAuth.Checked == true)
            {
                if (role.MenuFuncs == null)
                {
                    role.MenuFuncs = new List<MenuFunc>();
                }

                MenuFunc theFunc = authService.DaoGetVOById<MenuFunc>(int.Parse(hdnId.Value));

                if (!authService.RoleHasMenuRight(role, theFunc))
                {
                    role.MenuFuncs.Add(theFunc);
                }
            }
            else 
            {
                MenuFunc theFunc = authService.DaoGetVOById<MenuFunc>(int.Parse(hdnId.Value));

                if (authService.RoleHasMenuRight(role, theFunc))
                {
                    role.MenuFuncs.Remove(theFunc);
                }
            }
        }

        authService.UpdateRole(role);

        UserMenuFuncContainer.GetInstance().ResetAll() ;

        lblMsg.Text = MsgVO.UPDATE_OK;

    }
}


namespace bgo.web.ui
{
    public class UC14Service
    {
        AuthService authService = new AuthService();
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public UC14Service()
        {

        }


        public IList<RoleMenuShowVO> GetGridViewRow(string selectedRole, string selectedMenuFunc)
        {

            IList<RoleMenuShowVO> showVOList = new List<RoleMenuShowVO>();

            log.Debug("selectedRole=" + selectedRole);
            log.Debug("selectedMenuFunc=" + selectedMenuFunc);

            LoginRole role = authService.GetLoginRole(int.Parse(selectedRole));

            IList<MenuFunc> selectedMenuFuncList;


            if (selectedMenuFunc.Equals("0"))
            {
                selectedMenuFuncList = authService.GetNotTopMenuFunc();
            }
            else
            {
                selectedMenuFuncList = authService.GetMenuFuncByParentId(int.Parse(selectedMenuFunc));
            }

            foreach (MenuFunc menu in selectedMenuFuncList)
            {
                RoleMenuShowVO showVO = new RoleMenuShowVO();

                showVO.Id = menu.Id;
                showVO.Name = menu.MenuFuncName;
                showVO.No = menu.Note;
                if (role.MenuFuncs.Contains(menu))
                {
                    showVO.IsAuth = true;
                }
                else
                {
                    showVO.IsAuth = false;
                }

                showVOList.Add(showVO);
            }

            return showVOList;
        }

    }

    public class RoleMenuShowVO
    {
        public int Id { get; set; }
        public string No { get; set; }
        public string Name { get; set; }
        public bool IsAuth { get; set; }
    }
}